//
//  YSFlowOfferApplyViewController.m
//  YaSha-iOS
//
//  Created by YaSha_Tom on 2018/10/18.
//  Copyright © 2018年 亚厦装饰股份有限公司. All rights reserved.
//

#import "YSFlowOfferApplyViewController.h"
#import "YSFlowInterviewTableViewController.h"
#import "YSFormLinkViewController.h"
#import "YSFlowRecordListModel.h"
#import "YSNewsAttachmentModel.h"
#import "YSFlowRecordListCell.h"
#import "YSContactModel.h"

@interface YSFlowOfferApplyViewController ()

@property (nonatomic,strong)NSMutableArray *basicInformationArr;
@property (nonatomic,copy)NSDictionary *interviewDic;

@end

@implementation YSFlowOfferApplyViewController
# pragma mark -- 懒加载
- (NSMutableArray *)basicInformationArr {
    if (!_basicInformationArr) {
        _basicInformationArr = [NSMutableArray array];
    }
    return _basicInformationArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self doNetworking];
}
- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@/%@/%@", YSDomain, getOfferApplyApi, self.cellModel.businessKey, self.cellModel.processInstanceId];
    DLog(@"========%@",urlString);
    [QMUITips showLoadingInView:self.view];
    [YSNetManager ys_request_GETWithUrlString:urlString isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"offer发放申请详情:%@", response);
        [QMUITips hideAllToastInView:self.view animated:YES];
        if ([response[@"code"] intValue] == 1) {
            self.flowFormModel = [YSFlowFormModel yy_modelWithJSON:response[@"data"]];
            self.coverNavView.titleLabel.text = self.flowFormModel.baseInfo.title;
            [self.functionHeaderView setHeaderModel:self.flowFormModel.baseInfo];
            self.dataSource = self.flowFormModel.dataInfo;
            [self setUpData:self.flowFormModel.info];
            if ([self.flowFormModel.info[@"fileInfos"][@"file1"] count] > 0) {
                for (NSDictionary *dic in self.flowFormModel.info[@"fileInfos"][@"file1"]) {
                    YSNewsAttachmentModel *model = [YSNewsAttachmentModel yy_modelWithJSON:dic];
                    [self.attachArray addObject:model];
                }
            }
            if ([self.flowFormModel.info[@"remarks"][@"file2"] count] > 0) {
                for (NSDictionary *dic in self.flowFormModel.info[@"remarks"][@"file2"]) {
                    YSNewsAttachmentModel *model = [YSNewsAttachmentModel yy_modelWithJSON:dic];
                    [self.attachArray addObject:model];
                }
            }
            if (self.attachArray.count > 0) {
                [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",(unsigned long)self.attachArray.count] forState:UIControlStateNormal];
            }
            [self.postscriptArray addObject:@{@"title":@" ",@"content":self.flowFormModel.info[@"remarks"][@"remarks"],@"special":@"4"}];
            [self.tableView reloadData];
            //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
            dispatch_async(dispatch_get_main_queue(), ^{
                [self markSectionHeaderLocation];
            });
        }
    } failureBlock:^(NSError *error) {
        DLog(@"error:%@", error);
    } progress:nil];
    
//    //关联流程
//    NSString *urlStringAssociated = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getRelateFlowVo,self.cellModel.businessKey];
//    [YSNetManager ys_request_GETWithUrlString:urlStringAssociated isNeedCache:NO parameters:nil successBlock:^(id response) {
//        DLog(@"流程列表:%@", response);
//        self.associaterArr = [YSDataManager getFlowListData:response];
//        DLog(@"===222====%@",[YSDataManager getFlowListData:response]);
//        if (self.associaterArr.count > 0) {
//            [self.functionHeaderView.flowButton setTitle:[NSString stringWithFormat:@"关联流程(%lu)",(unsigned long)self.associaterArr.count] forState:UIControlStateNormal];
//        }
//    } failureBlock:^(NSError *error) {
//
//    } progress:nil];
//
//    //关联文档
//    NSString *urlStringDocument = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getAttachmentApi, self.cellModel.businessKey];
//    [YSNetManager ys_request_GETWithUrlString:urlStringDocument isNeedCache:NO parameters:nil successBlock:^(id response) {
//        DLog(@"获取关联文档:%@", response);
//        self.documentAr = [[YSDataManager getFlowAttachFileListData:response] copy];
//        if (self.documentAr.count > 0||self.fileArray.count > 0|| self.attachArray.count > 0) {
//            [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",(unsigned long)self.documentAr.count+self.fileArray.count+self.attachArray.count] forState:UIControlStateNormal];
//        }
//
//    } failureBlock:^(NSError *error) {
//        DLog(@"error:%@", error);
//    } progress:nil];
//
//    //提交者附言
//    NSString *urlStringPS = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getPostscriptListApi, self.cellModel.businessKey];
//    [YSNetManager ys_request_GETWithUrlString:urlStringPS isNeedCache:NO parameters:nil successBlock:^(id response) {
//        DLog(@"获取提交者附言:%@", response);
//        if ([response[@"code"] intValue] == 1) {
//
//            self.postscriptArray =[[YSDataManager getFlowAttachPSListData:response] copy];
//            [self.tableView reloadData];
//            //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self markSectionHeaderLocation];
//            });
//        }
//    } failureBlock:^(NSError *error) {
//        DLog(@"error:%@", error);
//    } progress:nil];
    
    //审批记录
    NSString *urlStringRecord = [NSString stringWithFormat:@"%@%@/%@/1", YSDomain, getCommentsByProcessInstanceIdApi, self.cellModel.processInstanceId];
    [YSNetManager ys_request_GETWithUrlString:urlStringRecord isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"处理列表:%@", response);
        self.recordArray = [[YSDataManager getFlowRecordListData:response] mutableCopy];
        [self.tableView reloadData];
        //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
        dispatch_async(dispatch_get_main_queue(), ^{
            [self markSectionHeaderLocation];
        });
        
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //转阅记录列表
    NSString *urlStringTurn = [NSString stringWithFormat:@"%@%@/%@/2", YSDomain, getCommentsByProcessInstanceIdApi, self.cellModel.processInstanceId];
    [YSNetManager ys_request_GETWithUrlString:urlStringTurn isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"处理列表:%@", response);
        self.turnArray = [[YSDataManager getFlowRecordListData:response] mutableCopy];
        [self.tableView reloadData];
        //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
        dispatch_async(dispatch_get_main_queue(), ^{
            [self markSectionHeaderLocation];
        });
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
}
# pragma mark -- 数据封装
- (void)setUpData:(NSDictionary *)dic {
    self.interviewDic = dic[@"interViewInfo"];
    [self.basicInformationArr addObject:@{@"title":@"基本信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"姓名",@"content":dic[@"applicant"][@"applicantName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"性别",@"content":dic[@"applicant"][@"gender"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"身份证号码",@"content":dic[@"applicant"][@"IdNumber"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"出生年月",@"content":dic[@"applicant"][@"birthday"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"毕业日期",@"content":dic[@"applicant"][@"graduationDate"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"毕业院校",@"content":dic[@"applicant"][@"graduationSchool"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"毕业专业",@"content":dic[@"applicant"][@"graduationMajor"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"全日制学历",@"content":dic[@"applicant"][@"education"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"职称/证书",@"content":dic[@"applicant"][@"certificate"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"劳动合同类型",@"content":dic[@"applicant"][@"contractType"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"招聘渠道",@"content":dic[@"applicant"][@"channel"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"任务编号",@"content":dic[@"applicant"][@"taskNumber"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"录用部门信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"所属业务",@"content":dic[@"applicantDepartment"][@"businessName"],@"special":@"0"}];
    if([dic[@"applicantDepartment"][@"businessName"] rangeOfString:@"装饰"].location !=NSNotFound){
        [self.basicInformationArr addObject:@{@"title":@"是否属于工业化",@"content":dic[@"applicantDepartment"][@"isIndustrialization"],@"special":@"0"}];
    }
    [self.basicInformationArr addObject:@{@"title":@"编制类型",@"content":dic[@"applicantDepartment"][@"establishmentName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"是否为仓管员",@"content":dic[@"applicantDepartment"][@"isWarehouse"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"录用部门",@"content":dic[@"applicantDepartment"][@"department"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"所带领导",@"content":dic[@"applicantDepartment"][@"leaderName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"职级",@"content":dic[@"applicantDepartment"][@"postLevel"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"职务名称",@"content":dic[@"applicantDepartment"][@"postName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"录用部门负责人",@"content":dic[@"applicantDepartment"][@"departmentPrincipalName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"录用部门中心副总",@"content":dic[@"applicantDepartment"][@"departmentVpName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"录用部门总经理",@"content":dic[@"applicantDepartment"][@"departmentGmName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"报道地点",@"content":dic[@"applicantDepartment"][@"reportSite"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"试用期信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"拟到岗日期",@"content":dic[@"applicantProbation"][@"arrivalPlanDate"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"试用期限",@"content":dic[@"applicantProbation"][@"probationEndDate"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"试用工资",@"content":dic[@"applicantProbation"][@"probationSalary"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"其他",@"content":dic[@"applicantProbation"][@"probationAnnualSalary"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"转正信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"目标全薪",@"content":dic[@"applicantConversion"][@"expectSalary"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"转正工资",@"content":dic[@"applicantConversion"][@"conversionSalary"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"其他",@"content":dic[@"applicantConversion"][@"conversionAnnualSalary"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"新进员工评估内容",@"content":dic[@"applicantConversion"][@"evaluationContent"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"附件信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"面试评价表",@"content":@"",@"special":@"2"}];
    [self.basicInformationArr addObject:@{@"title":@"应聘登记表",@"content":dic[@"interViewInfo"][@"ulApplyUrl"],@"special":@"2"}];
    
    self.dataSourceArray = self.basicInformationArr;
}
//在数据加载完后，从新设置定位位置
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self markSectionHeaderLocation];
} 
# pragma mark -- tableView deletage
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){
        NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", (long)indexPath.section,(long)indexPath.row];//以indexPath来唯一确定cell
        YSFlowFormListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[YSFlowFormListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        [cell setNewFlowDetailWithDictionary:self.dataSourceArray[indexPath.row]];
        return cell;
    }else if (indexPath.section == 1) {
        YSFlowFormListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; //出列可重用的cell
        if (cell == nil) {
            cell = [[YSFlowFormListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        [cell setNewFlowDetailWithDictionary:self.postscriptArray[indexPath.row]];
        return cell;
    }else if(indexPath.section == 2){
        YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellExaminationList]; //出列可重用的cell
        if (cell == nil) {
            cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellExaminationList];
        }
        if (self.recordArray.count > 0) {
            YSFlowRecordListModel *cellModel = self.recordArray[indexPath.row];
            [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
            [self callPhone:cellModel.userId andTableViewCell:cell];
        }
        return cell;
    }else{
        YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierFlow]; //出列可重用的cell
        if (cell == nil) {
            cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierFlow];
        }
        if (self.turnArray.count > 0) {
            YSFlowRecordListModel *cellModel = self.turnArray[indexPath.row];
            [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
            [self callPhone:cellModel.userId andTableViewCell:cell];
        }
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    if (indexPath.section == 0) {
        NSDictionary *dic = self.dataSourceArray[indexPath.row];
        if ([dic[@"special"] isEqualToString:@"2"]) {
            if ([dic[@"content"] length]>0) {
                YSFormLinkViewController *flowMapViewController = [[YSFormLinkViewController alloc] init];
                flowMapViewController.urlString = dic[@"content"];
                [self.navigationController pushViewController:flowMapViewController animated:YES];
            }else{
                YSFlowInterviewTableViewController *flowInterviewTableViewController = [[YSFlowInterviewTableViewController alloc]init];
                flowInterviewTableViewController.dic = self.interviewDic;
                [self.navigationController pushViewController:flowInterviewTableViewController animated:YES];
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
